package com.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.entity.FieldType;
import com.entity.TestCase;
import com.entity.TestFun;


@Repository
@Transactional
public class TestCaseDAO {
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<TestCase> listTestCase()
	{
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(
				TestCase.class);
		return cr.list();
	
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<String> listdistinct()
	{
		String hql = "select distinct datatree.parent from Tree datatree";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List results = query.list();
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public List<TestCase> listTestCaseByParent(String id)
	{
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(TestCase.class);
		Criterion cr1 = Restrictions.eq("parent",id);		
		cr.add(cr1);
		return cr.list();
		
	}
	
	@SuppressWarnings("unchecked")
	public List<TestCase> listTestCaseByName(String name)
	{
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(TestCase.class);
		Criterion cr1 = Restrictions.eq("testCaseName",name);		
		cr.add(cr1);
		return cr.list();
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<TestFun> listTestFunByName(String name)
	{
		
		
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(TestFun.class);
		Criterion cr1 = Restrictions.eq("tfName",name);		
		cr.add(cr1);
		return cr.list();
		
	
		
	}
	
	
	
	
	@SuppressWarnings("unchecked")
	public List<TestFun> listTestFunByparent(String id)
	{
		
		
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(TestFun.class);
		Criterion cr1 = Restrictions.eq("parent",id);		
		cr.add(cr1);
		return cr.list();
		
	
		
	}
	
	@SuppressWarnings("unchecked")
	public void saveTestCase(TestCase tc)
	{
		sessionFactory.getCurrentSession().save(tc);

	}

	
	@SuppressWarnings("unchecked")
	public List<FieldType> listFieldTypebyParentandTF(String tc,String tf)
	{
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(FieldType.class);
		Criterion cr1 = Restrictions.eq("testCaseName",tc);
		Criterion cr2 = Restrictions.eq("testFunName",tf);
		cr.add(Restrictions.and(cr1, cr2));
		return cr.list();
		
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<TestCase> listTestCaseByNameandParent(String name,String parent)
	{
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(TestCase.class);
		Criterion cr1 = Restrictions.eq("testCaseName",name);	
		Criterion cr2 = Restrictions.eq("parent",parent);
		cr.add(Restrictions.and(cr1, cr2));
		return cr.list();
		
	}
	
	@SuppressWarnings("unchecked")
	public void saveTestCaseFields(FieldType ft)
	{
		sessionFactory.getCurrentSession().save(ft);

	}
	
}
